package com.diqizu.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import com.diqizu.servlet.BaseServlet;
import com.diqizu.utli.Result;


import com.diqizu.dao.ChanPinDaoImpl;
import com.diqizu.entity.ChanPininof;
import com.diqizu.entity.LunBo;
import com.diqizu.servlet.BaseServlet;
import com.diqizu.utli.Result;

/**
 * Servlet implementation class JinXuanServleAdmian
 */
@WebServlet("/admin/JinXuanServleAdmian")
public class JinXuanServleAdmian extends BaseServlet {

  ChanPinDaoImpl cp=new ChanPinDaoImpl();
  public void getList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // 1.盗用dao层方法的方法获取数据
    ArrayList<ChanPininof> allList = cp.getALlcp();
    // 2.将数据封装为layu需要的格式
    String res = Result.toClient(0, "精选", allList, allList.size());
    PrintWriter out = resp.getWriter();
    out.write(res);
    out.flush();
    out.close();
  }

  public void insertcp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // 从页面上拿到数据
    String name = req.getParameter("name");
    String style = req.getParameter("style");
    String color = req.getParameter("color");
    String imga = req.getParameter("imga");
    String zhanshi = req.getParameter("zhanshi");
    // 调用dao层方法
    ChanPininof banner = new ChanPininof(0, name, style,color,imga,zhanshi);
    boolean res = cp.getALllb6(banner);
    PrintWriter out = resp.getWriter();
    if (res) {
      out.write(Result.toClient(0, "添加成功"));
    } else {
      out.write(Result.toClient(1, "添加失败"));
    }

    out.flush();
    out.close();
  }

  public void updatecp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // 从页面上拿到数据
    String id = req.getParameter("id");
    String name = req.getParameter("name");
    String style = req.getParameter("style");
    String color = req.getParameter("color");
    String imga = req.getParameter("imga");
    String zhanshi = req.getParameter("zhanshi");
    // 调用dao层方法
    ChanPininof banner = new ChanPininof(Integer.valueOf(id), name, style,color,imga,zhanshi);

    boolean res = cp.getALllb7(banner);
    PrintWriter out = resp.getWriter();
    if (res) {
      out.write(Result.toClient(0, "修改成功"));
    } else {
      out.write(Result.toClient(1, "修改失败"));
    }

    out.flush();
    out.close();
  }
  public void deletcp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // 从页面上拿到数据
    String id =req.getParameter("id");
    int Id=Integer.valueOf(id);
    // 调用dao层方法
    boolean res =cp.getALllb8(Id);
    
    PrintWriter out = resp.getWriter();
    if (res) {
      out.write(Result.toClient(0, "删除成功"));
    } else {
      out.write(Result.toClient(1, "删除失败"));
    }

    out.flush();
    out.close();
  }
}
